package com.central.apps.controller;

import com.central.app.service.HandlerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/Supuraltion")
public class SupuraltionController {
    @Autowired
    private HandlerService handlerService;
    @GetMapping("/Supuraltion")
    public Map<String,Object> Supuraltion(@RequestParam String supplierno,@RequestParam Integer page,@RequestParam Integer limit ){

        List<Map<String,Object>> bysql2= new ArrayList<>();

        List<Map<String, Object>> bySql = handlerService.findBySql("SELECT b.itemnum,b.itemdesc,b.model,b.orderunit,COUNT(*) as numabor from gq_po a inner join gq_poline b on a.ponum=b.ponum where a.suppliernum='"+supplierno+"' and a.status='已批准' group by b.itemnum,b.itemdesc,b.model,b.orderunit");
        if (CollectionUtils.isNotEmpty(bySql)){
            for (int i=0;i<bySql.size();i++){
                Map<String, Object> map = bySql.get(i);
                bysql2.add(map);
            }
        }
        List<Map<String,Object>> collect = bysql2.stream().skip((page-1)*limit).limit(limit).collect(Collectors.toList());
        HashMap<String, Object> hashMap = new HashMap<>();
        String code="0";
        hashMap.put("count",bysql2.size());
        hashMap.put("code",code);
        hashMap.put("data",collect);
        return hashMap;
    }
}
